/* 1. 模块根路径 */
const base: Record<string, string> = {
  menu: '/menus',
  user: '/users',
  dictTree: '/dict_tree',
  dictHeader:"/dict-headers",
  dictTable:"/dict-tables",
  dictValue:"/dict-values",
  objectHeader:"/objects",
  objectField:"/object-fileds",

};

/* 2. 动作 → 路径模板（带占位符） */
const actionMap = {
  /* ---------- select ---------- */
  'select': '',                 // GET /menus--按照page
  'selectById': '/:id',         // GET /menus/{id}
  'selectQuery': '/query',          // POST /menus/query --所有资料，带查询条件
  /*
    page: Optional[int] = 1
    page_size: Optional[int] = 100
    data: Optional[dict] = None  # 其他过滤字段
  */

  /* ---------- insert ---------- */
  'insert': '',               // POST /menus          单条
  'insertBatch': '/batch',    // POST /menus/batch    批量

  /* ---------- update ---------- */
  'updateById': '/:id',           // PUT /menus/{id}      单条
  'update': '',    // PUT /menus     批量

  /* ---------- delete ---------- */
  'delete': '',               // DELETE /menus

    /* ---------- mark ---------- */
  'mark': '',               // DELETE /menus

} as const;

/* 3. 工具：替换占位符 */
function fill(path: string, id?: string | number) {
  return id === undefined ? path : path.replace(':id', String(id));
}

/* 4. 核心：返回完整路径 */
export const apiUrl = {
  url(
    module: keyof typeof base,
    action: keyof typeof actionMap,
    id?: string | number
  ): string {
    const root = base[module];
    const tpl  = actionMap[action];
    return root + fill(tpl, id);
  },
};

/* 5. 类型导出 */
export type ModuleKey  = keyof typeof base;
export type ActionKey  = keyof typeof actionMap;
